Future<void> main() async {
  Function f = () => requestNameFromNet();
  ViewStateResponse res = await requestProxy(f);
  print(res);
}

Future<ViewStateResponse<T>> requestProxy<T>(Function request) async {
  // Response<T> res = await request.call();
  Future requestTask = Future(request);
  bool isTimeout = false;
  Response<T> res = await requestTask.timeout(
    Duration(seconds: 1),
    onTimeout: () {
      isTimeout = true;
      return null;
    },
  );
  if (isTimeout) {
    return ViewStateResponse(viewState: 1);
  }
  if (0 == res.code) {
    return ViewStateResponse(res: res);
  } else if (1 == res.code) {
    return ViewStateResponse(viewState: 1);
  } else {
    return ViewStateResponse(viewState: 2);
  }
}

Future<Response<String>> requestNameFromNet() async {
  await Future.delayed(Duration(seconds: 2));
  return Response<String>(0, "aaa");
}

class Response<T> {
  // 0 -> ok
  // 1 -> timeout
  // else -> server fail
  final int code;
  final T data;

  Response(this.code, this.data);

  @override
  String toString() {
    return 'Response{code: $code, data: $data}';
  }
}

class ViewStateResponse<T> {
  final Response<T> res;

  // 1 -> timeout
  // 2 -> server fail
  // null -> normal
  final int viewState;

  ViewStateResponse({this.res, this.viewState});

  @override
  String toString() {
    return 'ViewStateResponse{res: $res, viewState: $viewState}';
  }
}
